#!/usr/bin/python
from rap_core import *


# check all configurations in conflist
infile='conflist'
fin=open(infile,'r')
confs={}
for line in fin:
  if line.strip() == '': continue
  tmp=line.split('#')
  key=tmp[0].strip()
  try:
    ne=int(tmp[1])
  except:
    ne=None
  chk=tmp[2].strip()
  confs[key]=(chk,ne)
fin.close()
for key,(chk,ne) in confs.iteritems(): 
  try:
    newconf=rap_config(key,ne=ne)
    if str(newconf) != chk:
      print 'Problem with '+key+' (ne='+str(ne)+')'
      print 'Get '+newconf+' instead of '+chk
      print ''
  except:
    print 'Routine failed for '+key
    print ''


# define configuration by number of electrons
if str(rap_config(10)) != '2p6':
  print 'rap_config.__init__() with 10 electrons failed'


# define configuration with string
if str(rap_config('2p2')) != '2p2':
  print 'rap_config.__init__() failed'
if str(rap_config('2p2',ne=4)) != '2s0 2p2':
  print 'rap_config.__init__() failed'


# check equality
if not (rap_config(10) == rap_config('2p6')):
  print 'rap_config.__eq__() failed'
if not (rap_config(10) == '2p6'):
  print 'rap_config.__eq__() failed'


# check inequality
if rap_config(10) != rap_config('2p6'):
  print 'rap_config.__ne__() failed'
if rap_config(10) != '2p6':
  print 'rap_config.__ne__() failed'


# test comparison
c1=rap_config('2p4')
c2=rap_config('2p3 3s1')
if c1 > c2:
  print 'rap_config.__cmp__() failed'
if c2 < c1:
  print 'rap_config.__cmp__() failed'


# check hash
try:
  a={}
  a[rap_config(2)]=1
  a[rap_config(4)]=2
except:
  print 'rap_config.__hash__() failed'


# check length of configuration string
if len(rap_config('2s 2p4')) != 7:
  print 'rap_config.__len__() failed'


# check string addition
try:
  x=rap_config('2p6')+' hello'
  if x != '2p6 hello': raise
except:
  print 'rap_config.__add__() failed'
try:
  x='hello '+rap_config('2p6')
  if x != 'hello 2p6': raise
except:
  print 'rap_config.__radd__() failed'


# check label
conf=rap_config('2p6')
if conf.label() != '2p6':
  print 'rap_config.label() failed'
if conf.label(showclosed=True) != '1s2 2s2 2p6':
  print 'rap_config.label() failed'


# check electron count
if rap_config('2p6').num_electrons() != 10:
  print 'rap_config.num_electrons() failed'


# check subshell list
conf=rap_config('2p6')
if conf.subshells() != ['1s','2s','2p']:
  print 'rap_config.subshells() failed'
if conf.subshells(indices=True) != [1,2,3]:
  print 'rap_config.subshells() failed'


# check subshell occupancy
conf=rap_config('2p6')
if conf.occupancy(1) != 2:
  print 'rap_config.occupancy() failed'
if conf.occupancy('1s') != 2:
  print 'rap_config.occupancy() failed'


# check parity
if rap_config(9).parity() != 1:
  print 'rap_config.parity() failed'
if rap_config(10).parity() != 0:
  print 'rap_config.parity() failed'


# check weight
if rap_config('2s 2p3 3p').weight() != 240:
  print 'rap_config.weight() failed'


# check valence subshell
if rap_config(9).valence_subshell() != '2p':
  print 'rap_config.valence_subshell() failed'


# check pop electron
if rap_config(10).pop_electron() != rap_config(9):
  print 'rap_config.pop_electron() failed'
if rap_config(10).pop_electron(2) != rap_config('1s2 2s 2p6'):
  print 'rap_config.pop_electron() failed'
if rap_config(10).pop_electron('2s') != rap_config('1s2 2s 2p6'):
  print 'rap_config.pop_electron() failed'


# check difference
diff=rap_config(10).difference(rap_config(11).pop_electron(1))
try:
  if diff['1s'] != 1: raise
  if diff['3s'] != -1: raise
except:
  print 'rap_config.difference() failed'


# check sorting
confs=[]
confs.append(rap_config('1s2 2s1 2p6 3s'))
confs.append(rap_config('1s2 2s1 2p6 3s0 3p'))
confs.append(rap_config('1s2 2s2 2p6'))
confs.append(rap_config('1s2 2s2 2p5 3s0 3p'))
confs.append(rap_config('1s2 2s2 2p5 3s'))
confs.append(rap_config('1s1 2s2 2p6 3s0 3p'))
confs.append(rap_config('1s1 2s2 2p6 3s'))
confs.sort(rlf_confsorter)
if confs[0] != rap_config('1s2 2s2 2p6'):
  print 'rlf_confsorter failed'
if confs[-1] != rap_config('1s1 2s2 2p6 3s0 3p'):
  print 'rlf_confsorter failed'


# check list of allowed terms
conf=rap_config('2s1 2p2 3d1')
terms=conf.allowed_terms()
if len(terms) != 13:
  print 'rap_config.allowed_terms() failed'
if terms['3D'] != 4:
  print 'rap_config.allowed_terms() failed'


print '*done'
